package com.market.controller;

import com.market.common.exception.ControllerException;
import com.market.common.vo.ResponseCode;
import com.market.common.vo.ResponseResult;
import com.market.entity.Role;
import com.market.service.RoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author :  KanShan
 * @version : 1.8
 * @Date: 2021/12/01/20:57
 * @注释 :
 */
@RestController
public class RoleController {
    @Resource
    RoleService roleService;

    /**
     * 展示角色列表
     * @return
     * @throws ControllerException
     */

    @GetMapping("/role/find")
    public ResponseResult<List<Role>> findRole() throws ControllerException {
        List<Role> role = roleService.findRole();
        return ResponseResult.success(ResponseCode.SUCCESS, role);
    }

    /**
     * 通过名字 或者编号模糊查询角色信息
     * @param s
     * @return
     * @throws ControllerException
     */
    @GetMapping("/select/find")
    public ResponseResult<List<Role>> findRoleByIdOrName(@RequestParam String s) throws ControllerException {
        List<Role> roleByIdOrName = roleService.findRoleByIdOrName(s);
        return ResponseResult.success(ResponseCode.SUCCESS, roleByIdOrName);
    }

    /**
     * 插入角色信息
     * @param role
     * @return
     * @throws ControllerException
     */
    @PostMapping("/role/insert")
    public ResponseResult<Integer> insertRole(@RequestBody Role role) throws ControllerException {
        int i = roleService.insertRole(role);
        return ResponseResult.success(ResponseCode.SUCCESS, i);
    }

    /**
     * 修改角色信息
     * @param role
     * @return
     * @throws ControllerException
     */
    @PostMapping("/role/update")
    public ResponseResult<Integer> updateRole(@RequestBody Role role) throws ControllerException {
        int i = roleService.updateRole(role);
        return ResponseResult.success(ResponseCode.SUCCESS, i);
    }

    /**
     * 假删除
     * @param id
     * @return
     * @throws ControllerException
     */
    @PostMapping("/role/delete")
    public ResponseResult<Integer> deleteRole(@RequestParam Long id) throws ControllerException {
        int i = roleService.deleteRole(id);
        return ResponseResult.success(ResponseCode.SUCCESS, i);
    }
}
